//
//  LimitFreeTabBarViewController.m
//  LimitFree
//
//  Created by lijinghua on 16/2/29.
//  Copyright © 2016年 lijinghua. All rights reserved.
//

#import "LimitFreeTabBarViewController.h"
#import "TabBarModel.h"

@interface LimitFreeTabBarViewController ()

@property(nonatomic)NSMutableArray *tabBarModelArray;

@end

@implementation LimitFreeTabBarViewController

- (id)init{
    if (self = [super init]) {
        //初始化生成tabBarItem的model
        [self createTabBarModels];
        
        //根据tabBarModel生成对应的viewController
        [self createViewControllers];
    }
    return self;
}

- (void)createTabBarModels{
    self.tabBarModelArray = [NSMutableArray array];
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Controllers" ofType:@"plist"];
    NSArray *itemArray = [NSArray arrayWithContentsOfFile:plistPath];
    for (NSDictionary *dic in itemArray) {
        TabBarModel *model = [[TabBarModel alloc]initWithDictionary:dic error:nil];
        [self.tabBarModelArray addObject:model];
    }
}

- (void)createViewControllers{
    NSMutableArray *viewControllers = [NSMutableArray array];
    for (TabBarModel *model in self.tabBarModelArray) {
        //创建UIViewController
        NSString *className = model.className;
        UIViewController *viewController = [[NSClassFromString(className) alloc]init];
        viewController.title = model.title;
        
        //创建UINavigationController
        UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:viewController];
        nav.tabBarItem.title = model.title;
        nav.tabBarItem.image = [model image];
        
        //当设置selectedImage 时需要制定渲染模式
        //不制定使用tintColor来渲染图片
        
        //nav.tabBarItem.selectedImage
        [viewControllers addObject:nav];
    }
    self.viewControllers = viewControllers;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
